<?php

namespace app\model;

use think\facade\Db;
use think\Model;

class ActivitiesModel extends Model {

    protected $table ="activities";

    protected $pk = 'active_id';

    private $cacheKey="active:id:";

    private $cacheTime=5184000;


    /*
       * 获取total
       * */
    public function getTotal($where=[]){
        return Db::name($this->table)->where($where)->count();
    }


    /*
     * 获取list
     * */
    public function getList($where=[],$page = null,$limit = null,$orderField = null,$orderType = "ASC"){

        $obj =  Db::name($this->table)->field($this->pk)->where($where);

        if(!empty($page) && !empty($limit)){
            $obj = $obj->page($page, $limit);
        }

        if(!empty($orderField) && !empty($orderType)){
            $obj = $obj->order($orderField, $orderType);
        }

        $list = $obj->select();

        if($list){
            $return = [];
            foreach($list as $item){
                $return[] = $this->getInfo($item[$this->pk]);
            }
            return $return;
        }
        return [];
    }


    /*
   * 获取订单状态详情
   * */
    public function getInfo($id){
        $cacheKey = $this->cacheKey.$id;
        $redisModel = new RedisModel();
        $info = $redisModel->get($cacheKey);
        if($info){
            $info = json_decode($info,true);
        }else{
            $info = Db::name($this->table)->find($id);
            if($info){
                $redisModel->set($cacheKey,json_encode($info,256),$this->cacheTime);
            }
        }

        $info["is_can_sign_up"] = -1;
        $info["remaining_time"] = strtotime($info["stop_time"]) - time();
        if($info["active_status"] != -1 && strtotime($info["stop_time"]) <= time()) {
            $info["active_status"] = 2;
            $this->updateStatus($id, 2);
            $this->delCache($id);
        }else if($info["active_status"] != -1 &&  $info["active_max_num"] != 0 && $info["active_current_num"] >= $info["active_max_num"]){
            $info["active_status"] = 2;
            $this->updateStatus($id,2);
            $this->delCache($id);
        }elseif ($info["active_status"] != -1 &&  strtotime($info["stop_time"]) >= time()){

            //上架状态&截止报名时间> 0 && 报名人数未满
            if($info["active_status"] == 1 &&  $info["remaining_time"] > 0 && $info["active_max_num"] != 0 && $info["active_current_num"] < $info["active_max_num"]){
                $info["is_can_sign_up"]  =  1;
            }else if($info["active_status"] == 1 &&  $info["remaining_time"] > 0 && $info["active_max_num"] == 0) {
                $info["is_can_sign_up"] = 1;
            }
        }

        return $info;
    }



    /*
    * 添加
    * */
    public function addOne($data){
        return Db::name($this->table)->insertGetId($data);
    }


    /*
  * 添加
  * */
    public function updateOne($where,$data){
        return Db::name($this->table)->where($where)->update($data);
    }




    /*
  * 增加活动人数
  * */
    public function incrField($active_id,$field,$num = 1){
        if(empty($active_id) ){
            return false;
        }

        $where =[];
        $where[] =["active_id","=",$active_id] ;
        return Db::name($this->table)->where($where)->inc($field,$num)->update();
    }


    /*
    * 减少活动人数
    * */
    public function decField($active_id,$field,$num = 1){
        if(empty($active_id) ){
            return false;
        }

        $where =[];
        $where[] =["active_id","=",$active_id] ;
        return Db::name($this->table)->where($where)->dec($field,$num)->update();
    }



    /*
     * 更新活动状态
     * */
    public function updateStatus($active_id,$status)
    {
        $where =[];
        $where[] =["active_id","=",$active_id] ;
        return Db::name($this->table)->where($where)->update(["active_status"=>$status]);
    }




    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }

}